package com.es.index.product;

import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;

import java.io.IOException;
import java.util.List;

import org.elasticsearch.common.xcontent.XContentBuilder;
import org.springframework.beans.factory.annotation.Autowired;

import com.es.client.elasticesearch.ESClient;
import com.es.db.indexdb.bean.ProductIndex;
import com.es.db.indexdb.service.ProductService;
import com.fasterxml.jackson.databind.ObjectMapper;

public class ProductIndexHandler {
	private static final String INDEX = "indexdb";
	private static final String TYPE = "product";
	@Autowired
	private ProductService productService;
	ESClient esClient;
	private ProductIndexHandler(){
		esClient = ESClient.getEsClient();
		try {
			//Builder setting = setting();
			XContentBuilder mapping = mapping();
			XContentBuilder settingBuilder = settingBuilder();
			esClient.createIndex(INDEX, TYPE, mapping,settingBuilder);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	/**
	 * 设置当前索引的setting
	 * @return
	 * @throws IOException 
	 */
	/*private Builder setting() throws IOException {
		Builder setting = Settings.builder().put("index.number_of_shards", 3)//设置索引分片数量，索引创建后就不能修改分片数
				.put("index.number_of_replicas",2);//设置索引副本数量，这个可以更改副本数量
		//setting.put("blocks.read_only",true);//设置当前索引只允许读，不允许写或者更新操作
		//setting.put("blocks.read",true);//禁止对当前索引进行多操作
		//setting.put("blocks.write",true);//禁止对当前索引进行写操作
		settingBuilder();
		return setting;
	}*/
	
	
	public XContentBuilder settingBuilder() throws IOException {
		XContentBuilder settingsBuilder = jsonBuilder()
				.startObject()
				.field("index.number_of_shards",3).field("index.number_of_replicas",2)
				.startObject("analysis")
					.startObject("analyzer")
						.startObject("pinyin_analyzer")	.field("tokenizer", "pinyin_tokenizer").field("filter","word_delimiter").endObject()
					.endObject()
				.startObject("tokenizer")
					.startObject("pinyin_tokenizer")	.field("type", "pinyin").field("first_letter", "none").field("padding_char", " ").endObject()
				.endObject()
				.endObject()
				.endObject();
		return settingsBuilder;
	}
	
	private static class Holder{
		private static final ProductIndexHandler INSTANCE = new ProductIndexHandler();
	}
	
	public static ProductIndexHandler getInstance(){
		return Holder.INSTANCE;
	}
	
	public void create(int start, int limit) {
		List<ProductIndex> list = productService.getIndex(start, limit);
		for (ProductIndex index : list) {
			index(index);
		}
	}
	
	
	/**
	 * 创建索引
	 * @param goods
	 */
    public void index(ProductIndex index){
        // 创建索引
    	ObjectMapper mapper = new ObjectMapper();
        try {
        	String json = mapper.writeValueAsString(index);
        	String id = index.getProductid()+"";
        	esClient.createDoc(INDEX, TYPE, id, json);
        }catch (Exception e){
        	e.printStackTrace();
        }
    }
    
    /**
     * 创建索引mapping
     * @return
     * @throws IOException
     */
	private XContentBuilder mapping() throws IOException {
		//position_increment_gap 近似查询
		
		XContentBuilder builder = jsonBuilder()
				.startObject()
				.startObject("properties")
				.startObject("productid").field("type", "integer").endObject()
				.startObject("name").field("type", "text").field("analyzer", "ik_max_word").field("index_options","offsets").field("term_vector","with_positions_offsets").endObject()
				.startObject("pingyinname").field("type","text").field("analyzer","pinyin_analyzer").endObject()
				.startObject("storeid").field("type","integer").endObject()
				.startObject("createdate").field("type", "date").endObject()
				.startObject("column").field("type","integer").endObject()
				.startObject("priceValue").field("type","integer").endObject()
				.startObject("price").field("type","object").endObject()
				.endObject()
				.endObject();
		//.field("analyzer","pinyin_analyzer")
		/*private int ;
		private String ;
		private int ;
		private Date ;
		private List<Integer> column;
		private int priceValue;
		private Price price;*/
		return builder;
	}
    
    
   
   


}
